/*
 * @(#)xin.tianhui.cloud.weblog.core.thymeleaf.processor.element
 * @(#)ArticleProcessor.java	2018-04-06
 * 
 * Copyright © 2018-2027, All rights reserved.
 * homeanter
 */
package xin.tianhui.cloud.weblog.thymeleaf.processor;

import org.springframework.beans.factory.annotation.Autowired;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.templatemode.TemplateMode;

import xin.tianhui.cloud.weblog.core.model.dto.PagingDTO;
import xin.tianhui.cloud.weblog.core.model.vo.PagingVO;
import xin.tianhui.cloud.weblog.manager.IArticleMng;
import xin.tianhui.cloud.weblog.model.dto.ArticleResultDTO;
import xin.tianhui.cloud.weblog.model.dto.ArticleQueryDTO;
import xin.tianhui.cloud.weblog.thymeleaf.util.ThymeleafUtils;
import static org.thymeleaf.util.StringUtils.isEmptyOrWhitespace;

/**
 * 文章处理器
 * @ClassName: ArticleProcessor
 * @author junchen homeanter@163.com
 * @date 2018-04-06 22:55:23
 * @see TODO
 */
public class ArticleProcessor extends AbstractElementTagProcessor {

	private static final String ELEMENT_NAME = "article";
	private static final int PRECEDENCE = 100000;
	
	private static final String CHANNEL_BID_ATTR = "channelBid";
	
	private static final String IS_RECOMMEND_ATTR = "isRecommend";
	
	private static final String BID_ATTR = "bid";
	
	private static final String SORT_ATTR = "sort";
	
	private static final String SIDX_ATTR = "sidx";
	
	private static final String ROWS_ATTR = "rows";
	
	private static final String PAGE_ATTR = "page";
	
	@Autowired
	private IArticleMng articleMng;
	
	private Integer channelBid = null;
	private Integer isRecommend;
	private Integer bid = null;
	private Integer rows = null;
	private Integer page = null;
	private String sort = null;
	private String sidx = null;
	
	public ArticleProcessor(String dialectPrefix) {
		super(TemplateMode.HTML, dialectPrefix, ELEMENT_NAME, true, null, false, PRECEDENCE);
	}

	@Override
	protected void doProcess(ITemplateContext context, IProcessableElementTag elementTag,IElementTagStructureHandler handler) {
		building(context,elementTag);
		if(rows != null) {
			PagingDTO paging = new PagingDTO();
			paging.setRows(rows);
			paging.setPage(page == null ? 1 : page);
			if(!isEmptyOrWhitespace(sort) && (sort.toUpperCase().equals("DESC") || sort.toUpperCase().equals("ASC")) && !isEmptyOrWhitespace(sidx)) {
				paging.setSord(sort);
				paging.setSidx(sidx);
			}
			ArticleQueryDTO queryDto = new ArticleQueryDTO();
			if(channelBid!=null) {
				queryDto.setChannelBId(channelBid);
			}
			if(isRecommend != null) {
				queryDto.setIsRecommend(isRecommend);
			}
			PagingVO<ArticleResultDTO> pagingVO = articleMng.articlePaging(paging,queryDto);
			handler.setLocalVariable("paging", pagingVO);
		} else if(channelBid != null){
			
		}
		// 删除标签
		handler.removeTags();
	}
	
	private void building(ITemplateContext context, IProcessableElementTag elementTag) {
		this.channelBid = ThymeleafUtils.getValueAsInteger(context,elementTag,CHANNEL_BID_ATTR);
		this.bid = ThymeleafUtils.getValueAsInteger(context, elementTag, BID_ATTR);
		this.isRecommend = ThymeleafUtils.getValueAsInteger(context, elementTag, IS_RECOMMEND_ATTR);
		this.rows = ThymeleafUtils.getValueAsInteger(context,elementTag,ROWS_ATTR);
		this.page = ThymeleafUtils.getValueAsInteger(context,elementTag,PAGE_ATTR);
		this.sort = ThymeleafUtils.getValueAsStr(context,elementTag,SORT_ATTR);
		this.sidx = ThymeleafUtils.getValueAsStr(context,elementTag,SIDX_ATTR);
	}
}
